<?php
require_once '../../Common/Model/Helpper.php';
$log = new Helpper();
$log ->log("In user validation model: M_Validation.php", "True");
class Com_Validation extends Helpper
{   
    
    /*
     * return false if username is not correct or true otherwise
     * @return false
     */
    public function isUsername($username)
    {
         // return false and set error msg if username is emty or null
        if($username == '' || $username == NULL)
        {
            return false;
            //file_put_contents($log,"\r\nUsername is empty or null",FILE_APPEND);
        }
        /*
         * return false if username less than 8 or greater than 15 character
         * @return false
         */
        if(strlen($username)>15 || strlen($username)<8)
        {
            return false;
        }
        
        /*
         * return false if username contains other than numbers and letters
         * @return false
         */
        elseif(preg_match('/[^A-Za-z0-9]/',$username))
        {
            return false;
        }
        $this->log("Username is OK", "True");
        return true;
    }
    
    /*
     * return false if password is not correct or true otherwise
     * @return false
     */
    public function isPassword($password)
    {
        // return false password is emty or null
        if($password == '' || $password == NULL)
        {
            return false;
        }
        
        /*
         * return false if password less than 8 or greater than 15 character\
         * @return false
         */
        elseif(strlen($password)>15 || strlen($password)<8)
        {
            return false;
        }
        
        /*
         * return false if password contains other than numbers and letters
         * @return false
         */
        elseif(preg_match('/[^A-Za-z0-9]/',$password))
        {
            return false;
        }
        $this->log("Password is OK", "True");
        return true;
    }
    
    public function isName($name)
    {
         // return false if first name is emty or null
        if($name == '' || $name == NULL)
        {
            return false;
        }
        
        /*
         * return false if first name is less than 1 or greater than 20 character
         * @return false
         */
        if(strlen($name)>20 || strlen($name)<=1)
        {
            return false;
        }
        $this->log($name." is name", "True");
        return true;
    }
    
    public function isEmail($email)
    {
        // return false if email is empty or null
        if($email == '' || $email == NULL)
        {
            return false;
        }
        if(filter_var($email,FILTER_VALIDATE_EMAIL))
        {
            $this->log("Email is OK", "True");
            return true;
        }
        return false;
    }
    
    public function isMatchedPassword($password,$re_password)
    {
        if($re_password == '' || $re_password == NULL)
        {
            return false;
        }
        
        if($password == $re_password)
        {
            $this->log("Matched password", "True");
            return true;
        }
        return false;
    }
}
?>
